<?php
namespace app\admin\controller;
use think\Db;
class Order extends Base
{
    public function index(){
        if(request()->isAjax ()){
            extract(input());
            $map = [];
            if(isset($type)&&$type != ""){
                if($type == 1){
                    $map['o_id'] = ['eq',''];
                }else{
                    $map['o_id'] = ['neq',''];
                }
            }
            if(isset($role)&&$role != ""){
                $map['o_type'] = $role;
            }
            if(isset($status)&&$status != ""){
                $map['o_status'] = $status;
            }
            if(isset($key)&&$key!="")
            {
                $map['user_username|user_phone|o_order_num'] = ['like',"%" . $key . "%"];
            }
            if(isset($o_m_type)&&$o_m_type!="")
            {
                $map['o_m_type'] = $o_m_type;
            }
            if(session ('area')){
                $map['shop_qu_id'] = ['in',session('area')];
            }
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::table ("((SELECT o_id,user_id,shop_id,shop_qu_id,user_username,user_phone,o_order_num,o_type,o_shop_name,o_money,o_jifen,o_third_order_num,o_cate,o_express,o_express_num,o_add_time,o_pay_type,o_pay_status,o_status,o_m_type FROM up_order o LEFT JOIN up_u_user u ON u.user_id = o.o_user_id LEFT JOIN up_shop s ON s.shop_id = o.o_shop_id WHERE o_type IN (1,2)) UNION ALL (SELECT '' AS o_id,user_id,shop_id,shop_qu_id,user_username,user_phone,record_number AS o_order_num,IF(record_type != 1,1,2) AS o_type,shop_name AS o_shop_name,record_money AS o_money,record_jifen AS o_jifen,record_third_number AS o_third_order_num,'' AS o_cate,'' AS o_express,'' AS o_express_num,record_add_time AS o_add_time,record_payment_type AS o_pay_type,record_status AS o_pay_status,IF(record_status != 1,5,1) AS o_status,record_m_type AS o_m_type FROM up_record r LEFT JOIN up_u_user u ON u.user_id = r.record_user_id LEFT JOIN up_shop s ON s.shop_id = r.record_shop_id WHERE record_status IN (1,2))) AS t")
                ->where($map)
                ->count();
            // 
            $lists = Db::table ("((SELECT  '普通订单' `type`,o_id,user_id,shop_id,shop_qu_id,user_username,user_phone,o_order_num,o_type,o_shop_name,o_money,o_jifen,o_third_order_num,o_cate,o_express,o_express_num,o_add_time,o_pay_type,o_pay_status,o_status,o_m_type FROM up_order o LEFT JOIN up_u_user u ON u.user_id = o.o_user_id LEFT JOIN up_shop s ON s.shop_id = o.o_shop_id WHERE o_type IN (1,2)) UNION ALL (SELECT if(r.record_type = 3, '人脸订单', '扫码订单') `type`,'' AS o_id,user_id,shop_id,shop_qu_id,user_username,user_phone,record_number AS o_order_num,IF(record_type != 1,1,2) AS o_type,shop_name AS o_shop_name,record_money AS o_money,record_jifen AS o_jifen,record_third_number AS o_third_order_num,'' AS o_cate,'' AS o_express,'' AS o_express_num,record_add_time AS o_add_time,record_payment_type AS o_pay_type,record_status AS o_pay_status,IF(record_status != 1,5,1) AS o_status ,record_m_type AS o_m_type FROM up_record r LEFT JOIN up_u_user u ON u.user_id = r.record_user_id LEFT JOIN up_shop s ON s.shop_id = r.record_shop_id WHERE record_status IN (1,2))) AS t")
                ->field('t.*')
                ->where($map)
                ->page($Nowpage,$limits)
                ->order('o_add_time desc')
                ->select ();
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        return $this->fetch ('order/index');
    }


    public function orderInfo(){
        extract(input());
        if(request()->isAjax ()){
            $map = [];
            if(isset($key)&&$key!="")
            {
                $map['oi_goods_name'] = ['like',"%" . $key . "%"];
            }
            $map['oi_order_id'] = session('o_id');
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('order_info')
                ->where($map)
                ->count();
            $lists = Db::name('order_info')
                ->where($map)
                ->page($Nowpage,$limits)
                ->select ();
            if(!empty($lists)){
                foreach($lists as $key=>$vo){
                    $lists[$key]['oi_goods_image'] = explode (',',$vo['oi_goods_image']);
                }
            }
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        session('o_id',$id);
        return $this->fetch('order/order_info');
    }
}